package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.JTextField;

import dowork.DrawExcel;

public class WindowFramework extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private JPanel jPanel;
	private JButton selectImagePathButton;
	private JButton selectTargetPathButton;
	private JButton confirmButton;
	private JTextField filePathTextFiled;
	private JTextField imageWidthTextFiled;
	private JTextField fileTargetTextFiled;
	private JFileChooser jfChooser;

	public WindowFramework() {
		super();
		this.setTitle("Excel画板");
		jPanel = new JPanel();
		placeComponents(jPanel);
		this.add(jPanel);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(550, 150);
		this.setVisible(true);
	}

	private void placeComponents(JPanel jPanel) {
		
		jPanel.setLayout(null);
		
		JLabel imagePathLabel = new JLabel("图片的路径：");
		imagePathLabel.setBounds(10, 20, 80, 25);
		jPanel.add(imagePathLabel);

		filePathTextFiled = new JTextField(25);
		filePathTextFiled.setBounds(100, 20, 165, 25);
		jPanel.add(filePathTextFiled);

		selectImagePathButton = new JButton("浏览...");
		selectImagePathButton.addActionListener(this);
		selectImagePathButton.setBounds(300, 20, 80, 25);
		jPanel.add(selectImagePathButton);

		JLabel imageWidthLabel = new JLabel("生成宽度：");
		imageWidthLabel.setBounds(400, 20, 80, 25);
		jPanel.add(imageWidthLabel);
		
		imageWidthTextFiled = new JTextField(5);
		imageWidthTextFiled.setBounds(470, 20, 30, 25);
		imageWidthTextFiled.setText("300");
		jPanel.add(imageWidthTextFiled);
		
		JLabel outPathLabel = new JLabel("输出的路径：");
		outPathLabel.setBounds(10, 50, 80, 25);
		jPanel.add(outPathLabel);
		
		fileTargetTextFiled = new JTextField(25);
		fileTargetTextFiled.setBounds(100, 50, 165, 25);
		jPanel.add(fileTargetTextFiled);

		selectTargetPathButton = new JButton("浏览...");
		selectTargetPathButton.addActionListener(this);
		selectTargetPathButton.setBounds(300, 50, 80, 25);
		jPanel.add(selectTargetPathButton);
		
		confirmButton = new JButton("开始生成");
		confirmButton.addActionListener(this);
		confirmButton.setBounds(10, 80, 370, 25);
		jPanel.add(confirmButton);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton jButton = (JButton) e.getSource();
		if (jButton == selectImagePathButton) {
			jfChooser = new JFileChooser(new File("."));
			int status = jfChooser.showOpenDialog(this);
			if (status == JFileChooser.APPROVE_OPTION) {
				File file = jfChooser.getSelectedFile();
				filePathTextFiled.setText(file.getPath());
			}
		}
		if (jButton == selectTargetPathButton) {
			jfChooser = new JFileChooser(new File("."));
			jfChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int status = jfChooser.showOpenDialog(this);
			if (status == JFileChooser.APPROVE_OPTION) {
				File file = jfChooser.getSelectedFile();
				fileTargetTextFiled.setText(file.getPath());
			}
		}
		if (jButton == confirmButton) {
			DrawExcel drawExcel = new DrawExcel();
			try {
				FutureTask<Void> futureTask = new FutureTask<>(new Callable<Void>() {

					@Override
					public Void call() throws Exception {
						drawExcel.draw(filePathTextFiled.getText(), fileTargetTextFiled.getText(), imageWidthTextFiled.getText());
						return null;
					}
				});
				ExecutorService executorService = Executors.newFixedThreadPool(1);
				Future<?> future = executorService.submit(futureTask);
			
				JOptionPane.showMessageDialog(this, "正在处理...请按确定键并等待完成...");
				future.get();
				confirmButton.setText("开始生成");
				JOptionPane.showMessageDialog(this, "处理完成");
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(this, "出错了！"+e1.getMessage());
			}
		}
		
	}
}
